package com.rms.server.Controller;

import com.rms.server.Domain.Ingredient;
import com.rms.server.Service.IngredientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/ingredients")
@Tag(name = "原材料管理", description = "原材料管理相关接口")
public class IngredientController {

    @Autowired
    private IngredientService ingredientService;

    @PostMapping
    @Operation(summary = "添加原材料", description = "新增一个原材料")
    public String createIngredient(@RequestBody Ingredient ingredient) {
        int result = ingredientService.addIngredient(ingredient);
        return result > 0 ? "Ingredient added successfully!" : "Failed to add ingredient.";
    }

    @DeleteMapping("/{ingredientId}")
    @Operation(summary = "删除原材料", description = "根据 ID 删除指定原材料")
    public String deleteIngredient(@PathVariable Long ingredientId) {
        int result = ingredientService.removeIngredient(ingredientId);
        return result > 0 ? "Ingredient deleted successfully!" : "Failed to delete ingredient.";
    }

    @PutMapping
    @Operation(summary = "更新原材料", description = "根据传入的原材料对象更新原材料信息")
    public String updateIngredient(@RequestBody Ingredient ingredient) {
        int result = ingredientService.modifyIngredient(ingredient);
        return result > 0 ? "Ingredient updated successfully!" : "Failed to update ingredient.";
    }

    @GetMapping("/{ingredientId}")
    @Operation(summary = "查询单个原材料", description = "根据 ID 查询原材料详情")
    public Ingredient getIngredient(@PathVariable Long ingredientId) {
        return ingredientService.getIngredientById(ingredientId);
    }

    @GetMapping
    @Operation(summary = "查询所有原材料", description = "获取所有原材料的列表信息")
    public List<Ingredient> getAllIngredients() {
        return ingredientService.getAllIngredients();
    }
}